package com.sunjob.mroassets.mapper;

import com.sunjob.common.domain.assets.Instance;
import com.sunjob.common.domain.assets.Part;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface TimeMapper {

    //查询实例信息，得到实例列表
    @Select("select instance_id,instance_name,instance_model,time from t_instance")
    @Results({
            @Result(property = "instanceId",column = "instance_id"),
            @Result(property = "instanceName",column = "instance_name"),
            @Result(property = "instanceModel",column = "instance_model"),
            @Result(property = "time",column = "time")
    })
    List<Instance> getInstanceTime();

    //查询零件信息，得到零件列表
    @Select("select part_id,part_name,t_part.time,last_time,shape2_id,instance_name from t_part,t_instance" +
            " where t_part.instance_id = t_instance.instance_id")
    @Results({
            @Result(property = "partId",column = "part_id"),
            @Result(property = "partName",column = "part_name"),
            @Result(property = "time",column = "time"),
            @Result(property = "lastTime",column = "last_time"),
            @Result(property = "shape2Id",column = "shape2_id"),
            @Result(property = "message",column = "instance_name")
    })
    List<Part> getPartTime();

    //通过剩余寿命限制查询实例信息，得到实例列表
    @Select("select instance_id,instance_name,instance_model,time from t_instance where (time > #{startTime} and time <= #{endTime})")
    @Results({
            @Result(property = "instanceId",column = "instance_id"),
            @Result(property = "instanceName",column = "instance_name"),
            @Result(property = "instanceModel",column = "instance_model"),
            @Result(property = "time",column = "time"),
            @Result(property = "createDate",column = "create_date")
    })
    List<Instance> getInstanceTimeByLimity(int startTime,int endTime);

    //通过剩余寿命限制查询零件信息，得到零件列表
    @Select("select part_id,part_name,t_part.time,last_time,shape2_id,t_part.instance_id,instance_name from t_part,t_instance " +
            "where t_part.instance_id = t_instance.instance_id and (last_time > #{startTime} and last_time <= #{endTime})")
    @Results({
            @Result(property = "partId",column = "part_id"),
            @Result(property = "partName",column = "part_name"),
            @Result(property = "time",column = "time"),
            @Result(property = "lastTime",column = "last_time"),
            @Result(property = "shape2Id",column = "shape2_id"),
            @Result(property = "instanceId",column = "instance_id"),
            @Result(property = "message",column = "instance_name")
    })
    List<Part> getPartTimeByLimity(int startTime, int endTime);

    //修改实例列表中正常、故障状态的实例剩余寿命
    @Update("update t_instance set time = time-1 where time > 0")
    void updateInstanceTime();

    //修改部件列表中正常状态且剩余寿命不为空的部件剩余寿命
    @Update("update t_part set last_time = last_time-1 where (last_time is not null and last_time > 0)")
    void updatePartLastTime();

    //修改零件列表中正常状态的零件剩余寿命
    @Update("update t_accessory set last_time = last_time-1 where last_time > 0")
    void updateAccessoryLastTime();
}
